package com.chai.controller;

import com.chai.constant.MessageConstant;
import com.chai.constant.RedisMessageConstant;
import com.chai.utils.SMSUtils;
import com.chai.utils.ValidateCodeUtils;
import com.chai.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/ValidateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order")
    public Result Send4Order(String telephone){
        try {
            //生成校验码
            String code = ValidateCodeUtils.generateValidateCode4String(4);

            //发送短信
            SMSUtils.sendShortMessage(telephone,code);

            //将数据存储到redis
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,code);

            System.out.println("发送的手机号是："+code);

            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);

        }
    }
}
